﻿using Blm.Utils.Extensions;
using Blm.Utils.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Repository.Interface.Sys;

namespace VisionSurface.Service.Sys
{
    /// <summary>
    /// 用户服务类
    /// </summary>
    public class UserService
    {
        public ISysUserRepository SysUserRepository { get; set; }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        public SysUserDto FindUser(SysUserDto userDto)
        {
            SysUser record = SysUserRepository.Where(t => (t.AccountNumber == userDto.Code || t.PhoneNumber == userDto.Code || t.Email == userDto.Code)
                          && t.Password == userDto.Password.GetMD5() && t.IsDeleted == false).FirstOrDefault();

            if (record == null)
            {
                return null;
            }

            var dto = record.CopyTo<SysUserDto>();
            dto.RoleNameList = record.RoleNameList.ToObject<List<string>>() ?? new List<string>();
            dto.RoleList = new List<Guid>();
            dto.RoleCodeList = new List<string>();
            dto.SysOrgIds = (new string[] { record.SysOrgId }).ToList();
            dto.SysOrgCodes = (new string[] { record.CompanyNo }).ToList();
            return dto;
        }
    }
}
